package com.F04.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.F04.actionForm.F0404ActionForm;
import com.F04.actionForm.F0407ActionForm;
import com.F04.dao.F0404Dao;
import com.action.BaseAction;
import com.config.Constants;
import com.exception.SelectException;
import com.util.CommonTools;
import com.util.SessionUtils;

/**
 * �û��޸��������
 * 
 * @file F0404Action.java
 * @author wangjn
 * @since 2009-2-18
 * @version 1.0
 */
public class F0404Action extends BaseAction {
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		F0404ActionForm actionForm = (F0404ActionForm) form;
		F0404Dao dao = new F0404Dao();
		
//		供應商所屬家族
		actionForm.setList_pc_material_family(dao.List_pc_material_family());	
		String language = SessionUtils.getLocale(request);
		actionForm.setLanguage(language);
		String operationType = actionForm.getOperationType();
		
		try {
/*--------------------------------------------主頁面操作begin--------------------------------------------------*/
			
//			初始化
			if (Constants.INIT.equals(operationType)) {
				this.doINIT(actionForm);
				return mapping.findForward(Constants.List);
			}

//			返回(主頁面)
			if(Constants.BACK.equals(operationType)){
				return mapping.findForward(Constants.BACK);
			}

//			查询
			if (Constants.QUERYLIST.equals(operationType)) {
				List list = dao.select(actionForm);
				if(list != null){
					actionForm.setList(list);
				}else{
					actionForm.setList(new ArrayList());
				}
				return mapping.findForward(Constants.List);
			}

//			新增
			if(Constants.ADD.equals(operationType)){
				this.initActionForm(actionForm);
				actionForm.setErrorFlag("");
				return mapping.findForward(Constants.QUERYDETAIL);				
			}
			
//			編輯
			if(Constants.EDIT.equals(operationType)){
				actionForm.setSupplierCode(CommonTools.null2String(request.getParameter("code")));
				dao.selectByCode(actionForm);
				actionForm.setErrorFlag("");
				return mapping.findForward(Constants.QUERYDETAIL);
			}
			
//			刪除
			if(Constants.DELETE.equals(operationType)){
				dao.deleteByCode(actionForm);
				return mapping.findForward("operateReturnList");
			}

/*--------------------------------------------主頁面操作end--------------------------------------------------*/
		
			
			
			
/*--------------------------------------------明細頁面操作begin--------------------------------------------------*/


//			新增保存
			if(Constants.SAVE_INSERT.equals(operationType)){
				dao.insert(actionForm);
				return mapping.findForward("operateReturnList");	
			}			
			
//			編輯保存
			if(Constants.SAVE_UPDATE.equals(operationType)){
				dao.update(actionForm);
				return mapping.findForward("operateReturnList");
			}
			
//			明细页面返回主页面			if("detailBack".equals(operationType)){
				this.initActionForm(actionForm);
				actionForm.setErrorFlag("");
				return mapping.findForward(Constants.List);
			}
			
//			回显(新增和編輯保存后，返回主頁面)
			if("operateReturnList".equals(operationType)){
				actionForm.setSupplierName("");
				List list = dao.select(actionForm);
				if(list != null){
					actionForm.setList(list);
				}else{
					actionForm.setList(new ArrayList());
				}
				this.initActionForm(actionForm);
				actionForm.setErrorFlag("0");
				return mapping.findForward(Constants.List);				
			}

/*--------------------------------------------明細頁面操作end--------------------------------------------------*/	
			
			return mapping.findForward(Constants.List);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			this.initActionForm(actionForm);
			actionForm.setErrorFlag("3");
			e.printStackTrace();
			dao.rollback();
			return mapping.findForward(Constants.List);
		}finally {
			if (dao != null)
				dao.disConnect();
		}
	}

	/**
	 * @初始�?
	 */
	public void doINIT(F0404ActionForm actionForm) throws SelectException {
		actionForm.setCurrentPage(1);
	}
	
	public void initActionForm(F0404ActionForm actionForm){
		actionForm.setContractName("");
		actionForm.setFaxNumber("");
		actionForm.setPhoneNumber("");
		actionForm.setSupplierAddr("");
		actionForm.setSupplierCode("");
		actionForm.setSupplierName("");
		actionForm.setSupplierFamily("");
		actionForm.setVAT("");
	}
}
